package com.wt.admin.service.ai.impl.agents.easyai.api;

import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

public class EasyAIApiHelper {

    private EasyAIApiHelper() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }

    public static boolean isStreamingToolCall(EasyAIApi.ChatResponse ollamaChatResponse) {
        if (ollamaChatResponse != null && ollamaChatResponse.message() != null && ollamaChatResponse.message().toolCalls() != null) {
            return !CollectionUtils.isEmpty(ollamaChatResponse.message().toolCalls());
        } else {
            return false;
        }
    }

    public static boolean isStreamingDone(EasyAIApi.ChatResponse ollamaChatResponse) {
        if (ollamaChatResponse == null) {
            return false;
        } else {
            return ollamaChatResponse.done() && ollamaChatResponse.doneReason().equals("stop");
        }
    }

    public static EasyAIApi.ChatResponse merge(EasyAIApi.ChatResponse previous, EasyAIApi.ChatResponse current) {
        String model = merge(previous.model(), current.model());
        Instant createdAt = merge(previous.createdAt(), current.createdAt());
        EasyAIApi.Message message = merge(previous.message(), current.message());
        String doneReason = current.doneReason() != null ? current.doneReason() : previous.doneReason();
        Boolean done = current.done() != null ? current.done() : previous.done();
        Long totalDuration = merge(previous.totalDuration(), current.totalDuration());
        Long loadDuration = merge(previous.loadDuration(), current.loadDuration());
        Integer promptEvalCount = merge(previous.promptEvalCount(), current.promptEvalCount());
        Long promptEvalDuration = merge(previous.promptEvalDuration(), current.promptEvalDuration());
        Integer evalCount = merge(previous.evalCount(), current.evalCount());
        Long evalDuration = merge(previous.evalDuration(), current.evalDuration());
        return new EasyAIApi.ChatResponse(model, createdAt, message, doneReason, done, totalDuration, loadDuration, promptEvalCount, promptEvalDuration, evalCount, evalDuration);
    }

    private static EasyAIApi.Message merge(EasyAIApi.Message previous, EasyAIApi.Message current) {
        String content = mergeContent(previous, current);
        EasyAIApi.Message.Role role = current.role() != null ? current.role() : previous.role();
        role = role != null ? role : EasyAIApi.Message.Role.ASSISTANT;
        List<String> images = mergeImages(previous, current);
        List<EasyAIApi.Message.ToolCall> toolCalls = mergeToolCall(previous, current);
        return EasyAIApi.Message.builder(role).content(content).images(images).toolCalls(toolCalls).build();
    }

    private static Instant merge(Instant previous, Instant current) {
        return current != null ? current : previous;
    }

    private static Integer merge(Integer previous, Integer current) {
        if (previous == null) {
            return current;
        } else {
            return current == null ? previous : previous + current;
        }
    }

    private static Long merge(Long previous, Long current) {
        if (previous == null) {
            return current;
        } else {
            return current == null ? previous : previous + current;
        }
    }

    private static String merge(String previous, String current) {
        if (previous == null) {
            return current;
        } else {
            return current == null ? previous : previous + current;
        }
    }

    private static String mergeContent(EasyAIApi.Message previous, EasyAIApi.Message current) {
        if (previous != null && previous.content() != null) {
            if (current != null && current.content() != null) {
                String var10000 = previous.content();
                return var10000 + current.content();
            } else {
                return previous != null ? previous.content() : null;
            }
        } else {
            return current != null ? current.content() : null;
        }
    }

    private static List<EasyAIApi.Message.ToolCall> mergeToolCall(EasyAIApi.Message previous, EasyAIApi.Message current) {
        if (previous == null) {
            return current != null ? current.toolCalls() : null;
        } else {
            return current == null ? previous.toolCalls() : merge(previous.toolCalls(), current.toolCalls());
        }
    }

    private static List<String> mergeImages(EasyAIApi.Message previous, EasyAIApi.Message current) {
        if (previous == null) {
            return current != null ? current.images() : null;
        } else {
            return current == null ? previous.images() : merge(previous.images(), current.images());
        }
    }

    private static <T> List<T> merge(List<T> previous, List<T> current) {
        if (previous == null) {
            return current;
        } else if (current == null) {
            return previous;
        } else {
            List<T> merged = new ArrayList(previous);
            merged.addAll(current);
            return merged;
        }
    }


}
